package com.huifaapp.mall.activity.Homefragment.ShengqianActvity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.blankj.utilcode.util.SPUtils;
import com.google.gson.Gson;
import com.huifaapp.mall.R;
import com.huifaapp.mall.adapter.adapter2.ViewPagerAdatperpopwindow;
import com.huifaapp.mall.afinal.SPConstants;
import com.huifaapp.mall.api.ApiManager;
import com.huifaapp.mall.bean.Huishengqian.Detail;
import com.huifaapp.mall.uitils.AUniqueIdentifier;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import java.util.ArrayList;

public class NianHuaLvActivity extends AppCompatActivity implements View.OnClickListener {

    private ArrayList<View> views = new ArrayList<>();


    private ImageView mImgBack;
    /**
     * 了解年化收益
     */
    private TextView mTvTitle;
    /**
     * 好友邀请
     */
    private TextView mTvhy;
    /**
     * 0.00
     */
    private TextView mNumHy;
    private ImageView mImgHy;
    private RelativeLayout mRlTvhy;
    /**
     * 年化率
     */
    private TextView mTvnhl;
    /**
     * 3.84
     */
    private TextView mNumNhl;
    private ImageView mImgNhl;
    private RelativeLayout mRlTvnhl;
    /**
     * 消费能力
     */
    private TextView mTvxf;
    /**
     * 0.00
     */
    private TextView mNumXf;
    private ImageView mImgXf;
    private RelativeLayout mRlTvxf;
    /**
     * 购物行为
     */
    private TextView mTvgw;
    /**
     * 0.00
     */
    private TextView mNumGw;
    private ImageView mImgGw;
    private RelativeLayout mRlTvgw;
    /**
     * 分享频率
     */
    private TextView mTvfx;
    /**
     * 0.40
     */
    private TextView mNumFx;
    private ImageView mImgFx;
    private RelativeLayout mRlTvfx;
    /**
     * 使用频率
     */
    private TextView mTvsy;
    /**
     * 1.00
     */
    private TextView mNumSy;
    private ImageView mImgSy;
    private RelativeLayout mRlTvsy;
    private ImageView mImg;
    private RelativeLayout mRl;
    /**
     * 提高年化收益
     */
    private Button mBtn;
    private PopupWindow window;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_nian_hua_lv);
        initView();
        getnetdetail();


    }
        /*
                网络请求
         */
    private void getnetdetail() {
        String url = ApiManager.DETAIL+"?token="+ SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN)+"&appId="+ AUniqueIdentifier.getDeviceId(this);
        Log.e("年化率","主题url===="+url);
        HttpUtils http = new HttpUtils();
        http.send(HttpRequest.HttpMethod.POST, url, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                Detail detail = new Gson().fromJson(responseInfo.result, Detail.class);
                if (detail.getCode().equals("10000")){
                    Detail.DataBean data = detail.getData();
                    //好友邀请
                    double inviteMarks = data.getInviteMarks();
                    String inviteState = data.getInviteState();
                    mNumHy.setText(inviteMarks+"");
                    IsUpordwon(inviteState,mImgHy);
                    mNumHy.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            popwindow(0);
                        }
                    });
                    //消费能力
                    double consumeMarks = data.getConsumeMarks();
                    String consumeState = data.getConsumeState();
                    mNumXf.setText(consumeMarks+"");
                    IsUpordwon(consumeState,mImgXf);
                    mNumXf.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            popwindow(1);
                        }
                    });
                    //分享shipin
                    double shareMarks = data.getShareMarks();
                    String shareState = data.getShareState();
                    mNumFx.setText(shareMarks+"");
                    IsUpordwon(shareState,mImgFx);
                    mNumFx.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                           popwindow(2);
                        }
                    });
                    //使用频率
                    double useMarks = data.getUseMarks();
                    String useState = data.getUseState();
                    mNumSy.setText(useMarks+"");
                    IsUpordwon(shareState,mImgSy);
                    mNumSy.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                          popwindow(3);
                        }
                    });

                    //购物行为
                    double shoppingMarks = data.getShoppingMarks();
                    String shoppingState = data.getShoppingState();
                    mNumGw.setText(shoppingMarks+"");
                    IsUpordwon(shoppingState,mImgGw);
                    mNumGw.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                         popwindow(4);
                        }
                    });
                    //年化lv
                    double yearRate = data.getYearRate();
                    String yeatRateState = data.getYeatRateState();
                    mNumNhl.setText(yearRate+"");
                    IsUpordwon(yeatRateState,mImgNhl);
                    mNumNhl.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                          popwindow(5);
                        }
                    });

                }else {
                    Toast.makeText(NianHuaLvActivity.this, detail.getMsg(), Toast.LENGTH_SHORT).show();
                }

            }

            @Override
            public void onFailure(HttpException e, String s) {
                Toast.makeText(NianHuaLvActivity.this,s,Toast.LENGTH_SHORT).show();
            }
        });


    }

    private void IsUpordwon(String state,ImageView imager) {
        if (state.equals("up")){
            imager.setImageResource(R.drawable.shangsheng);
        }else {
            imager.setImageResource(R.drawable.jiantouxia3x);
        }

    }

    private void initView() {
        mImgBack = (ImageView) findViewById(R.id.Gvmine_back);
        mImgBack.setOnClickListener(this);
        mTvTitle = (TextView) findViewById(R.id.tv_title);
        mTvhy = (TextView) findViewById(R.id.tvhy);
        mTvhy.setOnClickListener(this);
        mNumHy = (TextView) findViewById(R.id.num_hy);
        mImgHy = (ImageView) findViewById(R.id.img_hy);
        mRlTvhy = (RelativeLayout) findViewById(R.id.rl_tvhy);
        mTvnhl = (TextView) findViewById(R.id.tvnhl);
        mNumNhl = (TextView) findViewById(R.id.num_nhl);
        mImgNhl = (ImageView) findViewById(R.id.img_nhl);
        mRlTvnhl = (RelativeLayout) findViewById(R.id.rl_tvnhl);
        mTvxf = (TextView) findViewById(R.id.tvxf);
        mNumXf = (TextView) findViewById(R.id.num_xf);
        mImgXf = (ImageView) findViewById(R.id.img_xf);
        mRlTvxf = (RelativeLayout) findViewById(R.id.rl_tvxf);
        mTvgw = (TextView) findViewById(R.id.tvgw);
        mNumGw = (TextView) findViewById(R.id.num_gw);
        mImgGw = (ImageView) findViewById(R.id.img_gw);
        mRlTvgw = (RelativeLayout) findViewById(R.id.rl_tvgw);
        mTvfx = (TextView) findViewById(R.id.tvfx);
        mNumFx = (TextView) findViewById(R.id.num_fx);
        mImgFx = (ImageView) findViewById(R.id.img_fx);
        mRlTvfx = (RelativeLayout) findViewById(R.id.rl_tvfx);
        mTvsy = (TextView) findViewById(R.id.tvsy);
        mTvsy.setOnClickListener(this);
        mNumSy = (TextView) findViewById(R.id.num_sy);
        mImgSy = (ImageView) findViewById(R.id.img_sy);
        mRlTvsy = (RelativeLayout) findViewById(R.id.rl_tvsy);
        mImg = (ImageView) findViewById(R.id.img);
        mRl = (RelativeLayout) findViewById(R.id.rl);
        mBtn = (Button) findViewById(R.id.btn);
        mBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.Gvmine_back:
                finish();
                break;
            case R.id.btn:
                Intent intent = new Intent(NianHuaLvActivity.this,TigaoNianhuaActivity.class);
                startActivity(intent);
                break;

        }


    }

    public  void  popwindow(int idex){
        View view =   LayoutInflater.from(NianHuaLvActivity.this).inflate(R.layout.tishengdilog, null, false);
        ViewPager  viewPager  = (ViewPager) view.findViewById(R.id.viewpager);
        LayoutInflater lf = getLayoutInflater().from(this);
        View view1 = lf.inflate(R.layout.viewpager_imagerview1, null);
        View view2 = lf.inflate(R.layout.viewpager_imagerview2, null);
        View view3 = lf.inflate(R.layout.viewpager_imagerview3, null);
        View view4 = lf.inflate(R.layout.veiwpager_imagerview4, null);
        View view5 = lf.inflate(R.layout.viewpager_imagerview5, null);
        view1.findViewById(R.id.duanbix).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                window.dismiss();
            }
        });
        view2.findViewById(R.id.duanbix).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                window.dismiss();
            }
        });
        view3.findViewById(R.id.duanbix).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                window.dismiss();
            }
        });
        view4.findViewById(R.id.duanbix).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                window.dismiss();
            }
        });
        view5.findViewById(R.id.duanbix).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                window.dismiss();
            }
        });
        views.add(view1);
        views.add(view2);
        views.add(view3);
        views.add(view4);
        views.add(view5);

        viewPager.setAdapter(new ViewPagerAdatperpopwindow(views));
        viewPager.setCurrentItem(idex,true);
        window = new PopupWindow(view, 800,1200,true);
        window.setOutsideTouchable(true);
        window.setTouchable(true);
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha = 0.7f;
        getWindow().setAttributes(lp);
        window.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                WindowManager.LayoutParams lp = getWindow().getAttributes();
                lp.alpha = 1f;
                getWindow().setAttributes(lp);
            }
        });

        window.showAtLocation(this.getWindow().getDecorView(), Gravity.CENTER, 0, 0);

    }




}
